#include "gofgame/dungeonkeeper/ui/StrategicOperationView.h"
#include "gofgame/common/ui/DockLayout.h"
#include "gofgame/common/ui/DockLayoutData.h"
#include "gofgame/common/ui/UIShellLayer.h"
#include "gofgame/common/graphics/LinearGradientBrush.h"
#include "gofgame/common/graphics/HornedTextureBrush.h"
#include "gofgame/dungeonkeeper/game/LevelScene.h"
#include "gofgame/dungeonkeeper/mapscene/MapScene.h"

using namespace gofgame::common::ui;
using namespace gofgame::dungeonkeeper::ui;
using namespace gofgame::dungeonkeeper::game;
using namespace gofgame::dungeonkeeper::mapscene;
using namespace babyfish;

StrategicOperationView::StrategicOperationView() {
	this->strategicDataView.initialize();
	this->backButton.initialize();
}

bool StrategicOperationView::init() {
	if (!Container::init()) {
		return false;
	}

	this->setLayout(new DockLayout(20));

	this->strategicDataView->init();
	this->addChild(this->strategicDataView);

	Ref<Container> container;
	container.initialize();
	container->init();
	container->setHeight(70);
	container->setLayout(new DockLayout());
	container->setLayoutData(new DockLayoutData(DockStyle_Bottom));
	this->addChild(container);

	this->backButton->init();
	this->backButton->setWidth(150);
	this->backButton->setBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			Brush::white()
		)
	);
	this->backButton->setPressedBackgroundBrush(
		new HornedTextureBrush(
			CCSize(10, 10),
			CCTextureCache::sharedTextureCache()->addImage(
				"gofgame/dungeonkeeper/ui/upgrade_button.png"
			),
			new LinearGradientBrush(0, 0, 0, 0, 1, 0, 0, 1)
		)
	);
	this->backButton->setLayoutData(new DockLayoutData(DockStyle_Right));
	container->addChild(this->backButton);
	this->backButton->click() += 
		event_handler<ComponentEventArgs>(this, &StrategicOperationView::backButton_click);
	return true;
}

void StrategicOperationView::onDispatchManualBubbleEvent(ManualBubbleEventArgs &e) {
	if (dynamic_cast<UnitUpgradeEventArgs*>(&e)) {
		this->onUnitUpgrade(static_cast<UnitUpgradeEventArgs&>(e));
	} else if (dynamic_cast<SkillUpgradeEventArgs*>(&e)) {
		this->onSkillUpgrade(static_cast<SkillUpgradeEventArgs&>(e));
	} else if (dynamic_cast<ProduceUpgradeEventArgs*>(&e)) {
		this->onProduceUpgrade(static_cast<ProduceUpgradeEventArgs&>(e));
	} else {
		Container::onDispatchManualBubbleEvent(e);
	}
}

void StrategicOperationView::onSkillUpgrade(SkillUpgradeEventArgs &e) {
	this->_skillUpgrade.invoke(this, e);
}

void StrategicOperationView::onUnitUpgrade(UnitUpgradeEventArgs &e) {
	this->_unitUpgrade.invoke(this, e);
}

void StrategicOperationView::onProduceUpgrade(ProduceUpgradeEventArgs &e) {
	this->_produceUpgrade.invoke(this, e);
}

void StrategicOperationView::backButton_click(void *sender, ComponentEventArgs &e) {
	MapScene *map = MapScene::create();
	CCDirector::sharedDirector()->replaceScene(map);
}